import mysql2, { PoolConnection } from 'mysql2'
import {
  MYSQL_HOST,
  MYSQL_PORT,
  MYSQL_DATABASE,
  MYSQL_USER,
  MYSQL_PASSWORD
} from './config'

// 创建连接
const connection = mysql2.createPool({
  host: MYSQL_HOST,
  port: Number(MYSQL_PORT),
  database: MYSQL_DATABASE,
  user: MYSQL_USER,
  password: MYSQL_PASSWORD
})

connection.getConnection(((err: NodeJS.ErrnoException, conn: PoolConnection) => {
  conn.connect(err => {
    if (err) {
      console.log('连接失败')
      return
    }
    console.log('数据库连接成功')
  })
}))

export default connection.promise()